
package model.staff;
import java.io.File;
import java.sql.Date;
import java.sql.ResultSet;


import javax.servlet.http.HttpSession;

import myutil.Connect;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;


public class DocumentUpload {
int latest_document_id=0;
int latest_message_id=0;
Connect c;
ResultSet rs;
	public void update_course_document_table(String comment,
			String subject, File attached_file, String file_name,int course_professor_id) 
	{
		
		// TODO Auto-generated method stub
		try 
		{
			HttpSession session = ServletActionContext.getRequest()
					.getSession();
			String uploader_email_id = session.getAttribute("username")
					.toString();
			if (attached_file != null) 
			{
				c = new Connect();
				// int uploader_id=0;
				
				c.dml("insert into document_upload(uploader_email_id,document_name) values('"
						+ uploader_email_id + "','" + file_name + "')");

				rs = c.getResult("select max(document_id) as max_id from document_upload");
				while (rs.next()) 
				{
					latest_document_id = rs.getInt("max_id");
					System.out.println(latest_document_id);
				}
				// System.out.println(getServlet().getServletContext().getRealPath(""));
				File f = new File(
						"F:/Metalink/attachments/"
								+ latest_document_id);
				f.mkdir();
			
				File f1 = new File(
						"F:/Metalink/attachments/"
								+ latest_document_id + "/" + file_name);
				FileUtils.copyFile(attached_file, f1);
			}
		  c= new Connect();
		  Date x;
		  
		int course_id=0;
		rs=c.getResult("select course_id from course_professor where course_professor_id="+course_professor_id);
		while(rs.next()){
		 course_id=rs.getInt("course_id");
		}
			
			c = new Connect();
			c.dml("insert into course_document(document_id,course_professor_id,course_id,uploader_email_id,comment,subject) values('"+latest_document_id+"',"+course_professor_id+","+course_id+",'"+uploader_email_id+"','"+comment+"','"+subject+"')");
			
		}
		catch (Exception e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}

}
